package com.zdp.oauth.config.oauth.authorization;

import com.zdp.oauth.entity.oauth.SignInIdentity;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;


/**
 * <h1>往access token 中添加附加信息</h1>
 */
public class CustomAccessTokenEnhancer implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        SignInIdentity signInIdentity = (SignInIdentity) authentication.getPrincipal();

        final Map<String, Object> additionalInfo = new HashMap<>();

        additionalInfo.put("fullname", signInIdentity.getFullname());
        additionalInfo.put("mobile", signInIdentity.getMobile());
        additionalInfo.put("id", signInIdentity.getId());

        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);

        return accessToken;

    }
}
